<template>
  <div class="role-manage">
    <el-card>
      <div class="header-bar">
        <span>角色列表</span>
        <el-button type="primary" size="small" @click="openAddRole">新增角色</el-button>
      </div>
      <el-table :data="roleList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="角色名称" />
        <el-table-column prop="code" label="角色编码" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="openEditRole(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDeleteRole(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑角色弹窗 -->
    <el-dialog v-model="roleDialogVisible" :title="roleForm.id ? '编辑角色' : '新增角色'">
      <el-form :model="roleForm" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="roleForm.code" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="roleForm.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="roleDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveRole">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getRoleList, addRole, updateRole, deleteRole } from '@/api/admin/role'

const roleList = ref([])
const roleDialogVisible = ref(false)
const roleForm = ref({ id: null, name: '', code: '', status: 1 })

const fetchRoles = async () => {
  const res = await getRoleList()
  if (res.code === 200) {
    roleList.value = res.data
  }
}

const openAddRole = () => {
  roleForm.value = { id: null, name: '', code: '', status: 1 }
  roleDialogVisible.value = true
}

const openEditRole = (row) => {
  roleForm.value = { ...row }
  roleDialogVisible.value = true
}

const handleSaveRole = async () => {
  if (!roleForm.value.name || !roleForm.value.code) {
    ElMessage.warning('角色名称和编码不能为空')
    return
  }
  let res
  if (roleForm.value.id) {
    res = await updateRole(roleForm.value)
  } else {
    res = await addRole(roleForm.value)
  }
  if (res.code === 200) {
    ElMessage.success('保存成功')
    roleDialogVisible.value = false
    fetchRoles()
  } else {
    ElMessage.error(res.msg || '保存失败')
  }
}

const handleDeleteRole = (row) => {
  ElMessageBox.confirm('确定要删除该角色吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deleteRole(row.id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      fetchRoles()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  }).catch(() => {})
}

onMounted(() => {
  fetchRoles()
})
</script>

<style scoped>
.role-manage {
  padding: 20px;
}
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
</style> 